package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.wheelview.NumericWheelAdapter;
import com.enqualcomm.kids.view.wheelview.WheelView;


import common.utils.DensityUtil;

/**
 * 编辑终端信息页面，生日对话框
 * @author yzq
 *
 */
public class MyDateTimePickerDialog3 extends Dialog implements
		View.OnClickListener{
	private final OnDateTimeSetListener mCallBack;
	private int windowWidth;
	private final WheelView wheelView_changeMinute;
	private final WheelView wheelView_changeSecond;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window dialogWindow = getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();


		lp.width =windowWidth - DensityUtil.dip2px(getContext(), 30); // 宽度
		dialogWindow.setAttributes(lp);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	public MyDateTimePickerDialog3(Context context,OnDateTimeSetListener callBack, String silencTime,String title) {
		super(context);
		mCallBack = callBack;
		// 找到dialog的布局文件
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_layout3, null);
		
		view.findViewById(R.id.sureBtn).setOnClickListener(this);
		view.findViewById(R.id.cancelBtn).setOnClickListener(this);
		
		TextView titleTv = (TextView) view.findViewById(R.id.titleText);
		titleTv.setText(title);

		DisplayMetrics dm = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWidth = dm.widthPixels;
		int textSize = DensityUtil.adjustFontSize(windowWidth);

		String[] timeArray = silencTime.split(":");
		wheelView_changeMinute = (WheelView) view.findViewById(R.id.hour);
		wheelView_changeMinute.setAdapter(new NumericWheelAdapter(0, 23));
		wheelView_changeMinute.setCyclic(true);
		wheelView_changeMinute.setCurrentItem(Integer.parseInt(timeArray[0]));
		wheelView_changeMinute.setLabel(context.getString(R.string.hour));
		wheelView_changeMinute.setTextSizeAndItemCount(textSize, 3);

		wheelView_changeSecond = (WheelView) view.findViewById(R.id.minute);
		wheelView_changeSecond.setAdapter(new NumericWheelAdapter(0, 59));
		wheelView_changeSecond.setCyclic(true);
		wheelView_changeSecond.setLabel(context.getString(R.string.minute));
		wheelView_changeSecond.setCurrentItem(Integer.parseInt(timeArray[1]));
		wheelView_changeSecond.setTextSizeAndItemCount(textSize, 3);

		setContentView(view);
	}
	public void onClick(View v) {
		if (v.getId() == R.id.cancelBtn) {
			dismiss();
		}else {
			if (mCallBack != null) {
				int hours = wheelView_changeMinute.getCurrentItem();
				int minute = wheelView_changeSecond.getCurrentItem();
				StringBuilder stringBuilder = new StringBuilder();
				if (hours < 10) {
					stringBuilder.append("0");
				}
				stringBuilder.append(hours).append(":");
				if (minute < 10) {
					stringBuilder.append("0");
				}
				stringBuilder.append(minute);

				mCallBack.onDateTimeSet(stringBuilder.toString());
			}
			dismiss();
		}
		
	}
	public  interface OnDateTimeSetListener {
		void onDateTimeSet( String newSilenceTime);
	}

}

